package com.yulongtian.interviewWorks.day20220826;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/implement-queue-using-stacks-lcci/
 *
 * @author YulongTian
 * @date 2022/8/26 - 9:50
 */
public class Test01 {
    public static void main(String[] args) {

    }
}

class MyQueue {
    static Stack<Integer> stackLeft = new Stack<>();
    static Stack<Integer> stackRight = new Stack<>();

    /**
     * Initialize your data structure here.
     */
    public MyQueue() {
        stackLeft.clear();
        stackRight.clear();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        if (stackRight.empty()) {
            stackLeft.push(x);
            return;
        }
        if (stackLeft.empty()) {
            while (!stackRight.empty()) {
                stackLeft.push(stackRight.pop());
            }
            stackLeft.push(x);
            return;
        }
        stackLeft.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        //一开始左栈不空
        if (!stackLeft.empty()) {
            //    左栈不为空
            while (!stackLeft.empty()) {
                stackRight.push(stackLeft.pop());
            }
            return stackRight.pop();
        }

        //然后右栈不空
        if (!stackRight.empty()) {
            int res = stackRight.pop();
            while (!stackRight.empty()) {
                stackLeft.push(stackRight.pop());
            }
            return res;
        }

        return -1;
    }

    /**
     * Get the front element.
     */
    public int peek() {
        //一开始左栈不空
        if (!stackLeft.empty()) {
            //    左栈不为空
            while (!stackLeft.empty()) {
                stackRight.push(stackLeft.pop());
            }
            return stackRight.peek();
        }

        //然后右栈不空
        if (!stackRight.empty()) {
            int res = stackRight.peek();
            while (!stackRight.empty()) {
                stackLeft.push(stackRight.pop());
            }
            return res;
        }

        return -1;
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return stackLeft.empty() && stackRight.empty();
    }
}
